Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(otlp/metrics): add in memory exporter and noop meter provider #127

Merged
merged 7 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ require (
github.com/go-sql-driver/mysql v1.8.1
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/grafana/otel-profiling-go v0.5.1
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/invopop/jsonschema v0.12.0
github.com/jackc/pgx/v5 v5.7.1
Expand Down Expand Up @@ -67,7 +66,8 @@ require (
go.temporal.io/api v1.39.0
go.temporal.io/sdk v1.29.1
go.temporal.io/sdk/contrib/opentelemetry v0.6.0
go.uber.org/fx v1.23.0
go.uber.org/dig v1.18.0
go.uber.org/fx v1.22.2
go.uber.org/mock v0.4.0
go.uber.org/zap v1.27.0
)
Expand Down Expand Up @@ -198,7 +198,6 @@ require (
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/otel/log v0.6.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/dig v1.18.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
Expand Down
13 changes: 2 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ github.com/go-faster/errors v0.7.1/go.mod h1:5ySTjWFiphBs07IKuiL69nxdfd5+fzh1u7F
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
Expand Down Expand Up @@ -187,8 +186,6 @@ github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/grafana/otel-profiling-go v0.5.1 h1:stVPKAFZSa7eGiqbYuG25VcqYksR6iWvF3YH66t4qL8=
github.com/grafana/otel-profiling-go v0.5.1/go.mod h1:ftN/t5A/4gQI19/8MoWurBEtC6gFw8Dns1sJZ9W4Tls=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys=
Expand Down Expand Up @@ -372,7 +369,6 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
Expand Down Expand Up @@ -437,7 +433,6 @@ go.opentelemetry.io/contrib/instrumentation/runtime v0.56.0 h1:s7wHG+t8bEoH7ibWk
go.opentelemetry.io/contrib/instrumentation/runtime v0.56.0/go.mod h1:Q8Hsv3d9DwryfIl+ebj4mY81IYVRSPy4QfxroVZwqLo=
go.opentelemetry.io/contrib/propagators/b3 v1.31.0 h1:PQPXYscmwbCp76QDvO4hMngF2j8Bx/OTV86laEl8uqo=
go.opentelemetry.io/contrib/propagators/b3 v1.31.0/go.mod h1:jbqfV8wDdqSDrAYxVpXQnpM0XFMq2FtDesblJ7blOwQ=
go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo=
go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY=
go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE=
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.31.0 h1:FZ6ei8GFW7kyPYdxJaV2rgI6M+4tvZzhYsQ2wgyVC08=
Expand All @@ -456,15 +451,12 @@ go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0 h1:UGZ1QwZWY67Z6Bm
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.31.0/go.mod h1:fcwWuDuaObkkChiDlhEpSq9+X1C0omv+s5mBtToAQ64=
go.opentelemetry.io/otel/log v0.6.0 h1:nH66tr+dmEgW5y+F9LanGJUBYPrRgP4g2EkmPE3LeK8=
go.opentelemetry.io/otel/log v0.6.0/go.mod h1:KdySypjQHhP069JX0z/t26VHwa8vSwzgaKmXtIB3fJM=
go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM=
go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE=
go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY=
go.opentelemetry.io/otel/sdk v1.21.0/go.mod h1:Nna6Yv7PWTdgJHVRD9hIYywQBRx7pbox6nwBnZIxl/E=
go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk=
go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0=
go.opentelemetry.io/otel/sdk/metric v1.31.0 h1:i9hxxLJF/9kkvfHppyLL55aW7iIJz4JjxTeYusH7zMc=
go.opentelemetry.io/otel/sdk/metric v1.31.0/go.mod h1:CRInTMVvNhUKgSAMbKyTMxqOBC0zgyxzW55lZzX43Y8=
go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ=
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
Expand All @@ -478,8 +470,8 @@ go.temporal.io/sdk/contrib/opentelemetry v0.6.0/go.mod h1:Lem8VrE2ks8P+FYcRM3Uph
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/dig v1.18.0 h1:imUL1UiY0Mg4bqbFfsRQO5G4CGRBec/ZujWTvSVp3pw=
go.uber.org/dig v1.18.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
go.uber.org/fx v1.23.0 h1:lIr/gYWQGfTwGcSXWXu4vP5Ws6iqnNEIY+F/aFzCKTg=
go.uber.org/fx v1.23.0/go.mod h1:o/D9n+2mLP6v1EG+qsdT1O8wKopYAsqZasju97SDFCU=
go.uber.org/fx v1.22.2 h1:iPW+OPxv0G8w75OemJ1RAnTUrF55zOJlXlo1TbJ0Buw=
go.uber.org/fx v1.22.2/go.mod h1:o/D9n+2mLP6v1EG+qsdT1O8wKopYAsqZasju97SDFCU=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
Expand Down Expand Up @@ -560,7 +552,6 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
47 changes: 18 additions & 29 deletions otlp/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,41 @@ package otlp
import (
"fmt"
"strings"
"sync"

flag "github.com/spf13/pflag"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
"go.uber.org/fx"
)

var (
onceInitOTLPFlags sync.Once
OnceLoadResources sync.Once
)

const (
OtelResourceAttributesFlag = "otel-resource-attributes"
OtelServiceNameFlag = "otel-service-name"
)

func AddFlags(flags *flag.FlagSet) {
onceInitOTLPFlags.Do(func() {
if flags.Lookup(OtelServiceNameFlag) == nil {
flags.String(OtelServiceNameFlag, "", "OpenTelemetry service name")
flags.StringSlice(OtelResourceAttributesFlag, []string{}, "Additional OTLP resource attributes")
})
}
}

func LoadResource(serviceName string, resourceAttributes []string) fx.Option {
options := make([]fx.Option, 0)
OnceLoadResources.Do(func() {
options = append(options,
fx.Provide(func() (*resource.Resource, error) {
defaultResource := resource.Default()
attributes := make([]attribute.KeyValue, 0)
if serviceName != "" {
attributes = append(attributes, attribute.String("service.name", serviceName))
return fx.Options(
fx.Provide(func() (*resource.Resource, error) {
defaultResource := resource.Default()
attributes := make([]attribute.KeyValue, 0)
if serviceName != "" {
attributes = append(attributes, attribute.String("service.name", serviceName))
}
for _, ra := range resourceAttributes {
parts := strings.SplitN(ra, "=", 2)
if len(parts) < 2 {
return nil, fmt.Errorf("malformed otlp attribute: %s", ra)
}
for _, ra := range resourceAttributes {
parts := strings.SplitN(ra, "=", 2)
if len(parts) < 2 {
return nil, fmt.Errorf("malformed otlp attribute: %s", ra)
}
attributes = append(attributes, attribute.String(parts[0], parts[1]))
}
return resource.Merge(defaultResource, resource.NewSchemaless(attributes...))
}),
)
})

return fx.Options(options...)
attributes = append(attributes, attribute.String(parts[0], parts[1]))
}
return resource.Merge(defaultResource, resource.NewSchemaless(attributes...))
}),
)
}
27 changes: 27 additions & 0 deletions otlp/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package otlp

import (
"github.com/spf13/cobra"
"go.uber.org/fx"
)

type Config struct {
ServiceName string
ResourceAttributes []string
}

func NewFxModule(cfg Config) fx.Option {
return fx.Options(
LoadResource(cfg.ServiceName, cfg.ResourceAttributes),
)
}

func FXModuleFromFlags(cmd *cobra.Command) fx.Option {
otelServiceName, _ := cmd.Flags().GetString(OtelServiceNameFlag)
otelResourceAttributes, _ := cmd.Flags().GetStringSlice(OtelResourceAttributesFlag)

return NewFxModule(Config{
ServiceName: otelServiceName,
ResourceAttributes: otelResourceAttributes,
})
}
64 changes: 29 additions & 35 deletions otlp/otlpmetrics/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
)

const (
OtelMetricsFlag = "otel-metrics"
OtelMetricsExporterPushIntervalFlag = "otel-metrics-exporter-push-interval"
OtelMetricsRuntimeFlag = "otel-metrics-runtime"
OtelMetricsRuntimeMinimumReadMemStatsIntervalFlag = "otel-metrics-runtime-minimum-read-mem-stats-interval"
OtelMetricsExporterFlag = "otel-metrics-exporter"
OtelMetricsKeepInMemoryFlag = "otel-metrics-keep-in-memory"
OtelMetricsExporterOTLPModeFlag = "otel-metrics-exporter-otlp-mode"
OtelMetricsExporterOTLPEndpointFlag = "otel-metrics-exporter-otlp-endpoint"
OtelMetricsExporterOTLPInsecureFlag = "otel-metrics-exporter-otlp-insecure"
Expand All @@ -24,44 +24,38 @@ const (
func AddFlags(flags *flag.FlagSet) {
otlp.AddFlags(flags)

flags.Bool(OtelMetricsFlag, false, "Enable OpenTelemetry traces support")
flags.Duration(OtelMetricsExporterPushIntervalFlag, 10*time.Second, "OpenTelemetry metrics exporter push interval")
flags.Bool(OtelMetricsRuntimeFlag, false, "Enable OpenTelemetry runtime metrics")
flags.Duration(OtelMetricsRuntimeMinimumReadMemStatsIntervalFlag, 15*time.Second, "OpenTelemetry runtime metrics minimum read mem stats interval")
flags.String(OtelMetricsExporterFlag, "stdout", "OpenTelemetry metrics exporter")
flags.String(OtelMetricsExporterOTLPModeFlag, "grpc", "OpenTelemetry traces OTLP exporter mode (grpc|http)")
flags.String(OtelMetricsExporterOTLPEndpointFlag, "", "OpenTelemetry traces grpc endpoint")
flags.Bool(OtelMetricsExporterOTLPInsecureFlag, false, "OpenTelemetry traces grpc insecure")
flags.String(OtelMetricsExporterFlag, "", "OpenTelemetry metrics exporter")
flags.String(OtelMetricsExporterOTLPModeFlag, "grpc", "OpenTelemetry metrics OTLP exporter mode (grpc|http)")
flags.String(OtelMetricsExporterOTLPEndpointFlag, "", "OpenTelemetry metrics grpc endpoint")
flags.Bool(OtelMetricsExporterOTLPInsecureFlag, false, "OpenTelemetry metrics grpc insecure")

// notes(gfyrag): apps are in charge of exposing in memory metrics using whatever protocol it wants to
flags.Bool(OtelMetricsKeepInMemoryFlag, false, "Allow to keep metrics in memory")
}

func FXModuleFromFlags(cmd *cobra.Command) fx.Option {
otelMetrics, _ := cmd.Flags().GetBool(OtelMetricsFlag)

if otelMetrics {
otelServiceName, _ := cmd.Flags().GetString(otlp.OtelServiceNameFlag)
otelMetricsExporterOTLPEndpoint, _ := cmd.Flags().GetString(OtelMetricsExporterOTLPEndpointFlag)
otelMetricsExporterOTLPMode, _ := cmd.Flags().GetString(OtelMetricsExporterOTLPModeFlag)
otelMetricsExporterOTLPInsecure, _ := cmd.Flags().GetBool(OtelMetricsExporterOTLPInsecureFlag)
otelMetricsExporter, _ := cmd.Flags().GetString(OtelMetricsExporterFlag)
otelMetricsRuntime, _ := cmd.Flags().GetBool(OtelMetricsRuntimeFlag)
otelMetricsRuntimeMinimumReadMemStatsInterval, _ := cmd.Flags().GetDuration(OtelMetricsRuntimeMinimumReadMemStatsIntervalFlag)
otelMetricsExporterPushInterval, _ := cmd.Flags().GetDuration(OtelMetricsExporterPushIntervalFlag)
otelResourceAttributes, _ := cmd.Flags().GetStringSlice(otlp.OtelResourceAttributesFlag)

return MetricsModule(ModuleConfig{
ServiceName: otelServiceName,
ServiceVersion: "develop",
OTLPConfig: &OTLPConfig{
Mode: otelMetricsExporterOTLPMode,
Endpoint: otelMetricsExporterOTLPEndpoint,
Insecure: otelMetricsExporterOTLPInsecure,
},
Exporter: otelMetricsExporter,
RuntimeMetrics: otelMetricsRuntime,
MinimumReadMemStatsInterval: otelMetricsRuntimeMinimumReadMemStatsInterval,
PushInterval: otelMetricsExporterPushInterval,
ResourceAttributes: otelResourceAttributes,
})
}
return fx.Options()
otelMetricsExporterOTLPEndpoint, _ := cmd.Flags().GetString(OtelMetricsExporterOTLPEndpointFlag)
otelMetricsExporterOTLPMode, _ := cmd.Flags().GetString(OtelMetricsExporterOTLPModeFlag)
otelMetricsExporterOTLPInsecure, _ := cmd.Flags().GetBool(OtelMetricsExporterOTLPInsecureFlag)
otelMetricsExporter, _ := cmd.Flags().GetString(OtelMetricsExporterFlag)
otelMetricsRuntime, _ := cmd.Flags().GetBool(OtelMetricsRuntimeFlag)
otelMetricsRuntimeMinimumReadMemStatsInterval, _ := cmd.Flags().GetDuration(OtelMetricsRuntimeMinimumReadMemStatsIntervalFlag)
otelMetricsExporterPushInterval, _ := cmd.Flags().GetDuration(OtelMetricsExporterPushIntervalFlag)
otelMetricsKeepInMemory, _ := cmd.Flags().GetBool(OtelMetricsKeepInMemoryFlag)

return MetricsModule(ModuleConfig{
OTLPConfig: &OTLPConfig{
Mode: otelMetricsExporterOTLPMode,
Endpoint: otelMetricsExporterOTLPEndpoint,
Insecure: otelMetricsExporterOTLPInsecure,
},
Exporter: otelMetricsExporter,
RuntimeMetrics: otelMetricsRuntime,
MinimumReadMemStatsInterval: otelMetricsRuntimeMinimumReadMemStatsInterval,
PushInterval: otelMetricsExporterPushInterval,
KeepInMemory: otelMetricsKeepInMemory,
})
}
81 changes: 81 additions & 0 deletions otlp/otlpmetrics/exporter_inmemory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package otlpmetrics

import (
"context"
"encoding/json"
"net/http"

sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
)

type InMemoryExporter struct {
exp sdkmetric.Exporter
metrics *metricdata.ResourceMetrics
}

func (e *InMemoryExporter) Temporality(kind sdkmetric.InstrumentKind) metricdata.Temporality {
if e.exp != nil {
return e.exp.Temporality(kind)
}

return sdkmetric.DefaultTemporalitySelector(kind)
}

func (e *InMemoryExporter) Aggregation(kind sdkmetric.InstrumentKind) sdkmetric.Aggregation {
if e.exp != nil {
return e.exp.Aggregation(kind)
}

return sdkmetric.DefaultAggregationSelector(kind)
}

func (e *InMemoryExporter) ForceFlush(ctx context.Context) error {
if e.exp != nil {
return e.exp.ForceFlush(ctx)
}

return nil
}

func (e *InMemoryExporter) Shutdown(ctx context.Context) error {
if e.exp != nil {
return e.exp.Shutdown(ctx)
}

return nil
}

func (e *InMemoryExporter) Export(ctx context.Context, data *metricdata.ResourceMetrics) error {
if err := ctx.Err(); err != nil {
return err
}

// notes(gfyrag): copy as indicate by interface sdkmetric.Exporter
// I don't know if that's enough...
e.metrics = &(*data)

return e.exp.Export(ctx, data)
}

func (e *InMemoryExporter) GetMetrics() *metricdata.ResourceMetrics {
return e.metrics
}

func NewInMemoryExporterDecorator(exp sdkmetric.Exporter) *InMemoryExporter {
return &InMemoryExporter{
exp: exp,
}
}

var _ sdkmetric.Exporter = (*InMemoryExporter)(nil)

func NewInMemoryExporterHandler(meterProvider *sdkmetric.MeterProvider, e *InMemoryExporter) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := meterProvider.ForceFlush(r.Context()); err != nil {
panic(err)
}

_ = json.NewEncoder(w).Encode(e.GetMetrics())
}
}
36 changes: 36 additions & 0 deletions otlp/otlpmetrics/exporter_noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package otlpmetrics

import (
"context"

sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
)

type NoOpExporter struct{}

func (e *NoOpExporter) Temporality(kind sdkmetric.InstrumentKind) metricdata.Temporality {
return sdkmetric.DefaultTemporalitySelector(kind)
}

func (e *NoOpExporter) Aggregation(kind sdkmetric.InstrumentKind) sdkmetric.Aggregation {
return sdkmetric.DefaultAggregationSelector(kind)
}

func (e *NoOpExporter) ForceFlush(ctx context.Context) error {
return nil
}

func (e *NoOpExporter) Shutdown(ctx context.Context) error {
return nil
}

func (e *NoOpExporter) Export(ctx context.Context, data *metricdata.ResourceMetrics) error {
return nil
}

func NewNoOpExporter() *NoOpExporter {
return &NoOpExporter{}
}

var _ sdkmetric.Exporter = (*NoOpExporter)(nil)
Loading