-
Notifications
You must be signed in to change notification settings - Fork 73
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(agent): add metrics to agent workers #3674
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package telemetry | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/kubeshop/tracetest/server/version" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.21.0" | ||
) | ||
|
||
func getAgentServiceName(serviceName string) string { | ||
return fmt.Sprintf("tracetest.agent-%s", serviceName) | ||
} | ||
|
||
func getResource(serviceName string) (*resource.Resource, error) { | ||
hostname, err := os.Hostname() | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get OS hostname: %w", err) | ||
} | ||
|
||
resource, err := resource.Merge( | ||
resource.Default(), | ||
resource.NewWithAttributes( | ||
semconv.SchemaURL, | ||
semconv.ServiceNameKey.String(serviceName), | ||
semconv.HostName(hostname), | ||
semconv.ServiceVersion(version.Version), // TODO: should we consider a version file for the agent? | ||
), | ||
) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("could not merge resources: %w", err) | ||
} | ||
|
||
return resource, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" | ||
"go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/metric/noop" | ||
metricsdk "go.opentelemetry.io/otel/sdk/metric" | ||
) | ||
|
||
const ( | ||
metricsReaderInterval = 30 * time.Second | ||
metricExporterTimeout = 5 * time.Second | ||
) | ||
|
||
func GetNoopMeter() metric.Meter { | ||
return noop.NewMeterProvider().Meter("noop") | ||
} | ||
|
||
func GetMeter(ctx context.Context, otelExporterEndpoint, serviceName string) (metric.Meter, error) { | ||
if otelExporterEndpoint == "" { | ||
// fallback, return noop | ||
return GetNoopMeter(), nil | ||
} | ||
|
||
realServiceName := getAgentServiceName(serviceName) | ||
|
||
provider, err := newMeterProvider(ctx, otelExporterEndpoint, realServiceName) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not create meter provider: %w", err) | ||
} | ||
|
||
return provider.Meter("tracetest.agent"), nil | ||
} | ||
|
||
func newMeterProvider(ctx context.Context, otelExporterEndpoint, serviceName string) (metric.MeterProvider, error) { | ||
resource, err := getResource(serviceName) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get resource: %w", err) | ||
} | ||
|
||
exporter, err := getMetricExporter(ctx, otelExporterEndpoint) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not create metric exporter: %w", err) | ||
} | ||
|
||
periodicReader := metricsdk.NewPeriodicReader( | ||
exporter, | ||
metricsdk.WithInterval(metricsReaderInterval), | ||
) | ||
|
||
provider := metricsdk.NewMeterProvider( | ||
metricsdk.WithResource(resource), | ||
metricsdk.WithReader(periodicReader), | ||
) | ||
|
||
return provider, nil | ||
} | ||
|
||
func getMetricExporter(ctx context.Context, otelExporterEndpoint string) (*otlpmetricgrpc.Exporter, error) { | ||
ctx, cancel := context.WithTimeout(ctx, metricExporterTimeout) | ||
defer cancel() | ||
|
||
exporter, err := otlpmetricgrpc.New(ctx, | ||
otlpmetricgrpc.WithEndpoint(otelExporterEndpoint), | ||
otlpmetricgrpc.WithCompressor("gzip"), | ||
otlpmetricgrpc.WithInsecure(), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not create metric exporter: %w", err) | ||
} | ||
|
||
return exporter, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This update is "taking" a ride with this PR. I'm updating the synthetic monitoring timeouts in a way that we can still send alarms on what is happening if they fail or become idle.