Skip to content

Commit

Permalink
feat(cmd): enhance OpenTelemetry resource detection (#155)
Browse files Browse the repository at this point in the history
Enrich OpenTelemetry resources with additional attributes

- Downgrade semconv from v1.27.0 to v1.26.0 to resolve version conflicts
- Add comprehensive resource detection including:
  - Service version
  - Process command args
  - Runtime information
  - Environment variables
  - SDK details
  - Process information
  - OS information
  - Container metadata
  - Host details
  • Loading branch information
kalbasit authored Dec 25, 2024
1 parent 6d256e1 commit 479750b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 15 deletions.
53 changes: 48 additions & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
sdklog "go.opentelemetry.io/otel/sdk/log"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"

"github.com/kalbasit/ncps/pkg/otelzerolog"
)
Expand Down Expand Up @@ -156,10 +156,10 @@ func setupOTelSDK(ctx context.Context, cmd *cli.Command) (func(context.Context)
prop := newPropagator()
otel.SetTextMapPropagator(prop)

res := resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName(cmd.Root().Name),
)
res, err := newResource(ctx, cmd)
if err != nil {
return shutdown, handleErr(err)
}

colURL := cmd.String("otel-grpc-url")
enabled := cmd.Bool("otel-enabled")
Expand Down Expand Up @@ -194,6 +194,49 @@ func setupOTelSDK(ctx context.Context, cmd *cli.Command) (func(context.Context)
return shutdown, nil
}

func newResource(ctx context.Context, cmd *cli.Command) (*resource.Resource, error) {
return resource.New(
ctx,

// Set the Schema URL.
// NOTE: This will fail if the semconv version being used within the
// deterctors is different and it's a check to ensure I am using the
// correct semconv version.
resource.WithSchemaURL(semconv.SchemaURL),

// Add Custom attributes.
resource.WithAttributes(
semconv.ServiceName(cmd.Root().Name),
semconv.ServiceVersionKey.String(Version),
),

// Discover and provide command-line information.
resource.WithProcessCommandArgs(),

// Discover and provide runtime information.
resource.WithProcessRuntimeVersion(),

// Discover and provide attributes from OTEL_RESOURCE_ATTRIBUTES and
// OTEL_SERVICE_NAME environment variables.
resource.WithFromEnv(),

// Discover and provide information about the OpenTelemetry SDK used.
resource.WithTelemetrySDK(),

// Discover and provide process information.
resource.WithProcess(),

// Discover and provide OS information.
resource.WithOS(),

// Discover and provide container information.
resource.WithContainer(),

// Discover and provide host information.
resource.WithHost(),
)
}

func newPropagator() propagation.TextMapPropagator {
return propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
Expand Down
42 changes: 32 additions & 10 deletions nix/packages/docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,41 @@
{
packages.docker = pkgs.dockerTools.buildLayeredImage {
name = "kalbasit/ncps";
contents = [
# required for TLS certificate validation
pkgs.cacert
contents =
let
etc-passwd = pkgs.writeTextFile {
name = "passwd";
text = ''
root:x:0:0:Super User:/homeless-shelter:/dev/null
'';
destination = "/etc/passwd";
};

# required for working with timezones
pkgs.tzdata
etc-group = pkgs.writeTextFile {
name = "group";
text = ''
root:x:0:
'';
destination = "/etc/group";
};
in
[
# required for Open-Telemetry auto-detection of process information
etc-passwd
etc-group

# required for migrating the database
pkgs.dbmate
# required for TLS certificate validation
pkgs.cacert

# the ncps package
config.packages.ncps
];
# required for working with timezones
pkgs.tzdata

# required for migrating the database
pkgs.dbmate

# the ncps package
config.packages.ncps
];
config = {
Cmd = [ "/bin/ncps" ];
Env = [
Expand Down

0 comments on commit 479750b

Please sign in to comment.