Skip to content

Commit

Permalink
feat(exporter/discordexporter): add discordexporter (#24)
Browse files Browse the repository at this point in the history
Fixes #16
  • Loading branch information
ymotongpoo authored Nov 17, 2023
1 parent 732a04b commit f3810c8
Show file tree
Hide file tree
Showing 9 changed files with 483 additions and 2 deletions.
45 changes: 45 additions & 0 deletions exporter/discordexporter/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2023 Yoshi Yamaguchi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package discordexporter

import (
"errors"

"go.opentelemetry.io/collector/component"
)

type Config struct {
// Token is Discord application token.
Token string `mapstructure:"token"`

// Channel is the target Discord channel to write data to.
Channel string `mapstructure:"channel"`
}

var _ component.Config = (*Config)(nil)

func createDeafultConfig() component.Config {
return &Config{}
}

func (c *Config) Validate() error {
if c.Token == "" {
return errors.New("token must be non-empty")
}
if c.Channel == "" {
return errors.New("channel must be non-empty")
}
return nil
}
57 changes: 57 additions & 0 deletions exporter/discordexporter/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2023 Yoshi Yamaguchi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package discordexporter

import (
"context"

"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/bwmarrin/discordgo"
)

type discordExporter struct {
config *Config
session *discordgo.Session
marshaler pmetric.JSONMarshaler
}

func newDiscordExporter(cfg *Config) (*discordExporter, error) {
s, err := discordgo.New("Bot " + cfg.Token)
if err != nil {
return nil, err
}
return &discordExporter{
session: s,
config: cfg,
}, nil
}

func (d *discordExporter) Shutdown(context.Context) error {
return d.session.Close()
}

func (d *discordExporter) pushMetrics(_ context.Context, md pmetric.Metrics) error {
buf, err := d.marshaler.MarshalMetrics(md)
if err != nil {
return err
}

_, err = d.session.ChannelMessageSend(d.config.Channel, string(buf))
if err != nil {
return err
}
return nil
}
54 changes: 54 additions & 0 deletions exporter/discordexporter/facgory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2023 Yoshi Yamaguchi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package discordexporter

import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"

metadata "github.com/ymotongpoo/opentelemetry-collector-extra/exporter/discordexporter/internal/metadata"
)

func NewFactory() exporter.Factory {
return exporter.NewFactory(
metadata.Type,
createDeafultConfig,
exporter.WithMetrics(createMetricsExporter, metadata.MetricsStability),
)
}

func createMetricsExporter(
ctx context.Context,
param exporter.CreateSettings,
cfg component.Config,
) (exporter.Metrics, error) {
c := cfg.(*Config)
de, err := newDiscordExporter(c)
if err != nil {
return nil, err
}
return exporterhelper.NewMetricsExporter(
ctx,
param,
cfg,
de.pushMetrics,
exporterhelper.WithShutdown(de.Shutdown),
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
)
}
46 changes: 46 additions & 0 deletions exporter/discordexporter/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module github.com/ymotongpoo/opentelemetry-collector-extra/receiver/discordreceiver

go 1.21.4

require (
github.com/bwmarrin/discordgo v0.27.1
go.opentelemetry.io/collector/component v0.89.0
go.opentelemetry.io/collector/consumer v0.89.0
go.opentelemetry.io/collector/exporter v0.89.0
)

require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.0.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector v0.89.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.89.0 // indirect
go.opentelemetry.io/collector/confmap v0.89.0 // indirect
go.opentelemetry.io/collector/extension v0.89.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0018 // indirect
go.opentelemetry.io/collector/pdata v1.0.0-rcv0018 // indirect
go.opentelemetry.io/otel v1.20.0 // indirect
go.opentelemetry.io/otel/metric v1.20.0 // indirect
go.opentelemetry.io/otel/trace v1.20.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
Loading

0 comments on commit f3810c8

Please sign in to comment.